package com.tao.controller;

import com.tao.entity.User;
import com.tao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;
    @RequestMapping("/reg")
    public String reg(User user){
        System.out.println("user = " + user);
        if (mapper.selectByUsername(user.getUsername())!=null){
            return "该用户已存在！<a href='/index.html'>返回首页</a>";
        }
        mapper.insert(user);
        return "注册成功！<a href='/index.html'>返回首页</a>";
    }
    @RequestMapping("/login")
    public String login(User user){
        System.out.println("user = " + user);
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            if (user.getPassword().equals(u.getPassword())){
                return "登陆成功！<a href='/index.html'>返回首页</a>";
            }else {
                return "密码错误！<a href='/login.html'>重新登陆</a>";
            }
        }
        return "该用户不存在！<a href='/login.html'>重新登陆</a>";
    }

    @RequestMapping("/check")
    public String check(String username){
        System.out.println("username = " + username);
        User u = mapper.selectByUsername(username);
        return u==null?"用户名可用":"用户名已存在";

    }
}
